import * as THREE from 'three';
import Tween from '../core/Tween';
/**
 * 游戏逻辑入口
 */
export default class Game {

    private scene: THREE.Scene;
    private camera: THREE.PerspectiveCamera;
    private renderer: THREE.WebGLRenderer;

    private touchNode: THREE.Object3D | undefined;

    constructor() {
        // let GUI = new window['dat'].GUI();//dat调试工具


        let scene = new THREE.Scene();//场景
        let camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 3000);//透视相机

        let renderer = new THREE.WebGLRenderer({ antialias: true });//渲染器

        renderer.gammaOutput = true;//不晓得是啥，后期需要查看一下
        renderer.setClearColor(0xcccccc);
        // renderer.setPixelRatio( window.devicePixelRatio );
        renderer.setPixelRatio(2);

        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);
        window.addEventListener('resize', () => {
            let width = window.innerWidth;
            let height = window.innerHeight;
            if (this.camera) {
                // 设置透视摄像机的长宽比
                this.camera.aspect = width / height;
                // 摄像机的 position 和 target 是自动更新的，而 fov、aspect、near、far 的修改则需要重新计算投影矩阵（projection matrix）
                this.camera.updateProjectionMatrix();
            }

            if (renderer) {
                renderer.setSize(width, height);
            }
        })

        this.scene = scene;
        this.camera = camera;
        this.renderer = renderer;
        this.timeTick();


        camera.position.z = 4.9583;
        camera.position.x = 0.3784;
        camera.position.y = -6.9258;
        camera.rotation.x = 58.4;
        camera.rotation.y = 0.0461;
        camera.rotation.z = 0.514;


        // //添加灯光
        // let light = new THREE.DirectionalLight(0xffffff, 1);
        let light = new THREE.AmbientLight('#ffffff', 1);

        const gltfLoader = new THREE.GLTFLoader();
        const url = 'res/model/majiangjiang.glb';


        gltfLoader.load(url, (gltf) => {
            console.log(gltf)
            this.scene = gltf.scene;

            this.scene.add(light);
            this.camera = gltf.cameras[0];
            this.camera.aspect = window.innerWidth / window.innerHeight;
            this.camera.updateProjectionMatrix();
        });

        gltfLoader.load('res/model/file.glb', (gltf) => {
            console.log(gltf)
        });



        let raycaster = new THREE.Raycaster();
        let mouse = new THREE.Vector3();
        let self = this;
        let selectedObject: any;

        window.addEventListener('touchend', onTouchEnd, false);

        // let t = new TimelineLite();

        function onTouchEnd(ev) {
            ev.preventDefault();
            // 点击获取屏幕坐标
            var event = ev.changedTouches[0]
            mouse.x = (event.clientX / window.innerWidth) * 2 - 1
            mouse.y = -(event.clientY / window.innerHeight) * 2 + 1

            raycaster.setFromCamera(mouse, self.camera)
            let intersects = raycaster.intersectObject(self.scene, true);

            if (selectedObject) {
                selectedObject = null;
            }

            if (intersects.length > 0) {
                var res = intersects.filter(function (res) {
                    return res && res.object;
                })[0];
                if (res && res.object) {
                    console.log(res.object)
                    self.touchNode = res.object;
                    
                }
            }
        }


    }


    /**
     * 刷新渲染
     */
    private timeTick() {
        requestAnimationFrame((time) => {
            this.timeTick();
            this.renderer.render(this.scene, this.camera);
            if (this.touchNode) {
                this.touchNode.rotation.y += 0.03;
            }
        });
    }
}